export const getFormatedDate = function (microTimestamp = Date.now()) {
    const date = new Date(microTimestamp);
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
        " " + date.getHours() + seperator2 + date.getMinutes() +
        seperator2 + date.getSeconds();
    return currentdate;
}

export const getUploadFilePromise = (tmpThumbUrl) => {
    let suffix = /\.[^.]+$/.exec(tmpThumbUrl)[0]; // 正则表达式，获取文件扩展名
    return new Promise((resolve, reject) => {
        wx.cloud.uploadFile({
            cloudPath: new Date().getTime() + suffix,
            filePath: tmpThumbUrl, // 文件路径
        }).then(res => {
            // get resource IDuyj
            var cloudThumbUrl = res.fileID;
            console.log("上传结果", cloudThumbUrl)
            resolve(cloudThumbUrl);
        })
    })
}

//将临时图片专为base64并在promise中resolve
export const fetchBase64src = function (tempFilePath, addtype = true) {

    return new Promise((resolve, reject) => {
        wx.getFileSystemManager().readFile({
            filePath: tempFilePath, //选择图片返回的相对路径
            encoding: 'base64', //编码格式
            success: resultBase => { //成功的回调
                const base64str = resultBase.data;
                // base64Data = wx.arrayBufferToBase64(wx.base64ToArrayBuffer(base64str));
                /// 拼接请求头，data格式可以为image/png或者image/jpeg等，看需求
                let base64src;
                if (addtype) {
                    base64src = "data:image/png;base64," + base64str;
                } else {
                    base64src = base64str;
                }
                resolve(base64src);
            }
        })
    })

}